package syntelos.tools;

import syntelos.sys.Init;
import syntelos.sys.Version;

import alto.sys.FileManager;
import alto.sys.Reference;
import alto.sys.Thread;

/**
 * 
 */
public class Server 
    extends syntelos.sys.Init
{
    public final static String DefaultConfigurationHTTPName = "SX/Server/HTTP";
    public final static String DefaultConfigurationHTTPResource = "resource:configuration-sx-server.txt";
    public final static java.lang.String[] DefaultConfiguration = {
        DefaultConfigurationHTTPName,
        DefaultConfigurationHTTPResource
    };
    public final static java.lang.String[][] Defaults = {
        DefaultConfiguration
    };
    public final static void Configure()
        throws java.io.IOException
    {
        java.lang.String[][] defaults = Defaults;
        for (int cc = 0, count = defaults.length; cc < count; cc++){
            java.lang.String[] argv = defaults[cc];
            Configure(argv);
        }
    }
    public final static void Configure(String[] argv)
        throws java.io.IOException
    {
        java.lang.String name = argv[0];
        java.lang.String resource = argv[1];
        Reference target = Init.Resources.ReferenceTo(name);
        Reference source = (new Reference(resource)).toRemote();

        if (DefaultConfigurationHTTPName == name && alto.sys.Options.Instance.hasPort()){
            syntelos.sys.PHeaders config = new syntelos.sys.PHeaders(source);
            config.readMessage();
            config.setHeader("Port",alto.sys.Options.Instance.getPort(config.getHeaderInteger("Port")));
            target.put(config);
        }
        else
            target.put(source);
        System.err.println("Configure> Server '"+name+"' from '"+resource+"'.");

    }
    public final static boolean NeedsConfigure()
        throws java.io.IOException
    {
        java.lang.String name = DefaultConfiguration[0];
        Reference target = Init.Resources.ReferenceTo(name);
        return (target.existsNotStorage());
    }


    public Server(){
        super();
    }

    public static void usage(java.io.PrintStream out){
        out.println("Usage");
        out.println();
        out.println("  Server");
        out.println();
        out.println("Description");
        out.println();
        out.println("  Tools style alias to server startup.");
        out.println();
        out.println("  Normally the server is started with ");
        out.println();
        out.println("     java -server -jar syntelos.jar");
        out.println();
        out.println("  However this class permits the following to ");
        out.println("  also function as expected");
        out.println();
        out.println("     java -server -jar syntelos.jar server");
        out.println();
        System.exit(1);
    }
    public static void main(String[] argv){
        if (alto.sys.Thread.In()){
            if (SInitServer()){
                Version.PrintlnLong("Syntelos");
                try {
                    Server init = new Server();
                    init.init(FileManager.Instance());
                }
                catch (Throwable t){
                    t.printStackTrace();
                    java.lang.System.exit(1);
                }
            }
            else {
                System.err.println(FailedSInitServer);
                System.exit(1);
            }
        }
        else {
            new Main(Server.class,argv).start();
        }
    }

}
